<?php
include "MysqliDB.php";
include "RedisDB.php";

//创建文件夹
function makedir($path){
    if (!is_dir($path)){
        mkdir($path);
    }
}

//带重试功能
function getContent($url, $retry=3, $sleep = 20){
    $curl = curl_init();
    $timeout = 30;
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); // 设置超时限制防止死循环
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');//解决某些网站中文乱码的问题
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));//解决某些网站采集不到内容的问题
    //请求内容和取得状态码
    $output = curl_exec($curl);
    $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    while(($http_code != 200 || empty($output)) && $retry--){
        sleep($sleep);
        $output = curl_exec($curl);
        $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    }
    $result['content'] = $output;
    $result['http_code'] = $http_code;
    curl_close($curl);
    return $result;
}

//提取一个页面的所有有效链接(检查场景)
function get_all_link($page, $domain_name){
    $pat = '/["\']http.*?["\']/i';
    preg_match_all($pat, $page, $a_array);
    $all_arr = array();
    foreach ($a_array[0] as $k=>$v){
        $v = substr(trim($v),1,strlen($v)-2);
        //待完善 todo
        if (!stripos($v,$domain_name) ||stripos($v,'mp4') || stripos($v,'script') || stripos($v,'pdf') || stripos($v,'doc') || stripos($v,'xhtml') || stripos($v,'.ico') || stripos($v,'.dtd') || stripos($v,'.jpg') || stripos($v,'.jpeg')  || stripos($v,'.png') || stripos($v,'.gif')){
            continue;
        }
        $all_arr[] = $v;
    }
    $all_arr = array_unique($all_arr);
    return $all_arr;
}

//unicode转码
function unicode_encode($str, $encoding = 'utf-8', $prefix = '&#', $postfix = ';') {
    $str = iconv($encoding, 'UCS-2BE', $str);
    $arrstr = str_split($str, 2);
    $unistr = '';
    for($i = 0, $len = count($arrstr); $i < $len; $i++) {
        $dec = hexdec(bin2hex($arrstr[$i]));
        $unistr .= $prefix . $dec . $postfix;
    }
    return $unistr;
}

/**
 * 从redis获得所需信息
 * 如果redis不存在则自动从mysql获取，并缓存到redis
 * @param $key
 * @param $sql
 * @param $type
 * @param $mysqldb
 * @return mixed
 */
function redisGet($key, $sql, $mysqldb, $type = 'all'){
    $redis = RedisDB::getRedis();

    if($redis->exists($key)){
        $data = $redis->get($key);
    }else{
        $db = MysqliDB::getIntance($mysqldb);
        if($type == 'one'){
            $data = $db->getOne($sql);
        }else if($type == 'row'){
            $data = $db->getRow($sql);
        }else{
            $data = $db->getAll($sql);
        }
        if(!empty($data)){
            $data = json_encode($data);
            $redis->set($key, $data);
            $redis->expire($key,1800);
        }
    }
    if ($type != 'one'){
        $data = object_to_array(json_decode($data));
    }else{
        $data = json_decode($data);
    }
    return $data;
}

//对象转数组
function object_to_array($obj) {
    $obj = (array)$obj;
    foreach ($obj as $k => $v) {
        if (gettype($v) == 'resource') {
            return null;
        }
        if (gettype($v) == 'object' || gettype($v) == 'array') {
            $obj[$k] = (array)object_to_array($v);
        }
    }
    return $obj;
}

//总控更新客户端数据方法
function getData($client_id, $client_ip){
    //服务端调用接口地址
    $url = "http://$client_ip";
    $array['server_ip'] = $_SERVER['SERVER_ADDR'];
    $array['api'] = 'dataApi';
    $array['token'] = '';
    $result = object_to_array(json_decode(curl_post($url,$array)));
    if ($result['code'] == 200){
        $data = $result['data'];
        $sql = "INSERT INTO `domain` (`id`, `client_id`, `domain_name`, `obj_site`, `title`, `keyword`, `description`, `core_word`) VALUES ";
        foreach ($data as $k => $row){
            $id = $client_id.'0'.$data[$k]['group_id'].'0'.$data[$k]['id'];
            $domain_name = $data[$k]['domain_name'];
            $obj_site = $data[$k]['obj_site'];
            $title = str_replace("'","''",$data[$k]['title']);
            $keyword = str_replace("'","''",$data[$k]['keyword']);
            $description = str_replace("'","''",$data[$k]['description']);
            $core_word = str_replace("'","''",$data[$k]['core_word']);
            $sql .= "($id, $client_id, '$domain_name', '$obj_site', '$title', '$keyword', '$description', '$core_word'),";
        }
        $sql = rtrim($sql,',');
        $del_sql = "delete from `domain` where client_id = $client_id";
        $db = MysqliDB::getIntance('fnserver');
        $db->query($del_sql);
        $db->query($sql);
        return 1;
    }else{
        return 0;
    }
}

/**
 * url:访问路径
 * array:要传递的数组
 * */
function curl_post($url,$array){
    $curl = curl_init();
    //设置提交的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    //设置post数据
    $post_data = $array;
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //获得数据并返回
    return $data;
}